﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：项目库
 */
var selectedRow;
var refreshGirdData;
var grid;
var chart_a;
var chart_b;

var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {

            $("#gridtable_a").height($(window).height() - 75);
            $("#gridtable_b").height($(window).height() - 75);

            page.bind();
            page.initleft();
            page.initGrid_a();
            //page.initChart_a();
            page.initGrid_b();
        },
        bind: function () {
            var myDate = new Date();
            $("#year").val(myDate.getFullYear());
            $("#year").change(function () {
                page.search();
                //page.loadChart_a();
            });

            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 分配
            $('#lr_allocation').on('click', function () {
                var year = $("#year").val();
                var keyValue = $('#gridtable_a').jfGridValue('id');
                selectedRow = $('#gridtable_a').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form_allocation1',
                        title: '费用分配【' + selectedRow.name + '】',
                        url: config.webportal_server + 'pages/pbd/budget/allocation2/form.html?keyValue=' + keyValue + '&year=' + year + '&access_token=' + config.getToken().access_token,
                        width: 980,
                        height: 740,
                        btn: null
                        // callBack: function (id) {
                        //     return top[id].acceptClick(refreshGirdData);
                        // }
                    });
                }
            });
            // 查看
            $('#lr_view').on('click', function () {
                var year = $("#year").val();
                var keyValue = $('#gridtable_a').jfGridValue('id');
                selectedRow = $('#gridtable_a').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form_allocation1',
                        title: '查看分配【' + selectedRow.name + '】',
                        url: config.webportal_server + 'pages/pbd/budget/allocation2/view.html?keyValue=' + keyValue + '&year=' + year + '&access_token=' + config.getToken().access_token,
                        width: 980,
                        height: 740,
                        btn: null
                        // callBack: function (id) {
                        //     return top[id].acceptClick(refreshGirdData);
                        // }
                    });
                }
            });
        },
        initleft: function () {
            $('#lr_left_list li').on('click', function () {
                var $this = $(this);
                var $parent = $this.parent();
                $parent.find('.active').removeClass('active');
                $this.addClass('active');

                var data_value = $this.attr('data-value');

                switch (data_value) {
                    case "1":
                        grid = $("#gridtable_a");
                        $("#div_a").show();
                        $("#div_b").hide();

                        $('#lr_allocation').show();
                        break;
                    case "2":
                        grid = $("#gridtable_b");
                        $("#div_a").hide();
                        $("#div_b").show();

                        $('#lr_allocation').hide();
                        break;
                    default:
                        grid = $("#gridtable_a");
                        $("#div_a").show();
                        $("#div_b").hide();

                        $('#lr_allocation').show();
                        break;
                }

                page.search();
            });
        },
        initGrid_a: function () {
            $('#gridtable_a').jfGrid({
                url: config.base_server() + 'pbdprojectas/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '项目名称', name: 'name', width: 160, align: 'left'},
                    {
                        label: '项目类别', name: 'category', width: 80, align: 'center',
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('dataItem', {
                                key: value,
                                code: 'ProjectCategorys',
                                callback: function (_data) {
                                    callback(_data.text);
                                }
                            });
                        }
                    },
                    {
                        label: '项目级别', name: 'level', width: 80, align: 'center',
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('dataItem', {
                                key: value,
                                code: 'ProjectLevels',
                                callback: function (_data) {
                                    callback(_data.text);
                                }
                            });
                        }
                    },
                    {label: '用款单位', name: 'useUnit', width: 120, align: 'left'},
                    {label: '项目起始年份', name: 'startYear', width: 80, align: 'center'},
                    {label: '项目终止年份', name: 'endYear', width: 80, align: 'center'},
                    {
                        label: '项目财政金额', name: 'fiscalBalance', width: 100, align: 'right',
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {
                        label: '项目总额', name: 'money', width: 100, align: 'right',
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {label: '项目单位', name: 'proUnit', width: 100, align: 'left'},
                    {
                        label: '存续状态', name: 'existenceStatus', width: 100, align: 'center',
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('dataItem', {
                                key: value,
                                code: 'ProjectExistenceStatus',
                                callback: function (_data) {
                                    callback(_data.text);
                                }
                            });
                        }
                    },
                    {
                        label: '项目来源', name: 'source', width: 100, align: 'center',
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('dataItem', {
                                key: value,
                                code: 'ProjectSources',
                                callback: function (_data) {
                                    callback(_data.text);
                                }
                            });
                        }
                    },
                    {label: '申报单位', name: 'applyUnit', width: 100, align: 'left'},
                    {
                        label: '是否科研项目', name: 'isScience', width: 100, align: 'center',
                        formatter: function (cellvalue, row) {
                            if (cellvalue == 1) {
                                return '<span class=\"label label-success\" style=\"cursor: pointer;\">是</span>';
                            } else {
                                return '<span class=\"label label-default\" style=\"cursor: pointer;\">否</span>';
                            }
                        }
                    },
                    {label: '申报属性', name: 'attribute', width: 100, align: 'left'},
                    {
                        label: '当年余额', name: 'currentBalance', width: 100, align: 'right',
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {label: '上年项目预算安排情况', name: 'preDescription', width: 180, align: 'left'},
                    {
                        label: "更新时间", name: "createTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: '项目id', name: 'id', width: 160, align: 'center', hidden: true},
                ],
                dblclick: function (row) {
                    var year = $("#year").val();
                    var keyValue = $('#gridtable_a').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'form',
                            title: '查看分配',
                            url: config.webportal_server + 'pages/pbd/budget/allocation2/view.html?keyValue=' + keyValue + '&year=' + year + '&access_token=' + config.getToken().access_token,
                            width: 980,
                            height: 740,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isPage: true,
                reloadSelected: true,
                mainId: 'id'
            });

            page.search();
        },
        initGrid_b: function () {
            $('#gridtable_b').jfGrid({
                url: config.base_server() + 'pbdprojectbs/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '项目名称', name: 'name', width: 160, align: 'center'},
                    {
                        label: '项目类别', name: 'category', width: 80, align: 'center',
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('dataItem', {
                                key: value,
                                code: 'ProjectCategorys',
                                callback: function (_data) {
                                    callback(_data.text);
                                }
                            });
                        }
                    },
                    {
                        label: '项目级别', name: 'level', width: 80, align: 'center',
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('dataItem', {
                                key: value,
                                code: 'ProjectLevels',
                                callback: function (_data) {
                                    callback(_data.text);
                                }
                            });
                        }
                    },
                    {label: '用款单位', name: 'useUnit', width: 120, align: 'left'},
                    {label: '项目起始年份', name: 'startYear', width: 80, align: 'center'},
                    {label: '项目终止年份', name: 'endYear', width: 80, align: 'center'},
                    {
                        label: '项目财政金额', name: 'fiscalBalance', width: 100, align: 'right',
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {
                        label: '项目总额', name: 'money', width: 100, align: 'right',
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {label: '项目单位', name: 'proUnit', width: 100, align: 'left'},
                    {
                        label: '存续状态', name: 'existenceStatus', width: 100, align: 'center',
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('dataItem', {
                                key: value,
                                code: 'ProjectExistenceStatus',
                                callback: function (_data) {
                                    callback(_data.text);
                                }
                            });
                        }
                    },
                    {
                        label: '项目来源', name: 'source', width: 100, align: 'center',
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('dataItem', {
                                key: value,
                                code: 'ProjectSources',
                                callback: function (_data) {
                                    callback(_data.text);
                                }
                            });
                        }
                    },
                    {label: '申报单位', name: 'applyUnit', width: 100, align: 'left'},
                    {
                        label: '是否科研项目', name: 'isScience', width: 100, align: 'center',
                        formatter: function (cellvalue, row) {
                            if (cellvalue == 1) {
                                return '<span class=\"label label-success\" style=\"cursor: pointer;\">是</span>';
                            } else {
                                return '<span class=\"label label-default\" style=\"cursor: pointer;\">否</span>';
                            }
                        }
                    },
                    {label: '申报属性', name: 'attribute', width: 100, align: 'left'},
                    {
                        label: '当年余额', name: 'currentBalance', width: 100, align: 'right',
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {label: '上年项目预算安排情况', name: 'preDescription', width: 180, align: 'left'},
                    {
                        label: "更新时间", name: "createTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    }
                ],
                dblclick: function (row) {
                    var year = $("#year").val();
                    var keyValue = $('#gridtable_b').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'view',
                            title: '查看分配',
                            url: config.webportal_server + 'pages/pbd/budget/allocation2/view.html?keyValue=' + keyValue + '&year=' + year + '&access_token=' + config.getToken().access_token,
                            width: 980,
                            height: 740,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isPage: true,
                reloadSelected: true,
                mainId: 'id'
            });

            page.search();
        },
        initChart_a: function () {
            var dom = document.getElementById("chart_a");
            chart_a = echarts.init(dom);
            var option = {
                dataset: {
                    source: []
                },
                grid: {containLabel: false},
                xAxis: {name: 'amount'},
                yAxis: {type: 'category'},
                series: [
                    {
                        type: 'bar',
                        encode: {
                            // Map the "amount" column to X axis.
                            x: 'amount',
                            // Map the "product" column to Y axis
                            y: 'product'
                        }
                    }
                ]
            };

            if (option && typeof option === 'object') {
                chart_a.setOption(option);
            }
        },
        loadChart_a: function () {
            var year = $("#year").val();
            learun.httpAsyncGet(config.base_server() + 'pbdbudgetadjust1s/getChart?year=' + year, function (res) {
                learun.loading(false);
                if (res.resp_code == 200) {
                    chart_a.setOption({        //加载数据图表
                        dataset: {
                            source:
                            res.data

                        },
                    });

                }
            });
        },
        search: function (param) {
            param = param || {};
            var year = $("#year").val();
            param.year = year;
            var data_value = $('#lr_left_list').find('.active').attr('data-value');
            switch (data_value) {
                case "1":
                    $('#gridtable_a').jfGridSet('reload', param);
                    page.loadChart_a();
                    break;
                case "2":
                    $('#gridtable_b').jfGridSet('reload', param);
                    break;
            }
        }
    };

    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


